// pages/evaluate/index.js
const service = require("../../service/evaluate")
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast.js';
const uploadImage = require("../../utils/uploadImg/uploadImg.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileList: [],
    size: 0,
    value1: 0,
    value2: 0,
    message: "",
    spuId: "",
    orderDetAllId: "",
    showBtn: true,
    commentPicList:[],
  },
  afterRead: function (e) {
    console.log(e);
    var list = this.data.fileList;
    list.push({
      url: e.detail.file.path,
    });
    this.setData({
      fileList: list,
    });
  },
  deletephoto: function (event) {
    const {
      index,
      name
    } = event.detail;
    const fileList = this.data.fileList;
    fileList.splice(index, 1);
    this.setData({
      fileList: fileList,
    });
  },
  onChange1(event) {
    this.setData({
      value1: event.detail,
    });
  },
  onChange2(event) {
    this.setData({
      value2: event.detail,
    });
  },
  onChange3(event) {
    // event.detail 为当前输入的值
    this.setData({
      message:event.detail,
      size: event.detail.length
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      spuId: options.spuId ? options.spuId : "",
      orderDetAllId: options.orderDetAllId ? options.orderDetAllId : ""
    })
    if (options.id) {
      this.setData({
        showBtn: false
      })
      service.searchComment(options.id, res => {
        console.log(res)
        res.data.commentPicList.forEach(item => {
          item.url = item.picUrl
        })
        this.setData({
          value1: res.data.score,
          value2: res.data.pdScore,
          message: res.data.comments,
          fileList: res.data.commentPicList,
          size: res.data.comments.length
        })
      })
    }
  },
  publish: function () {
    if (this.data.value1 == 0) {
      Toast.fail("请评价商品！")
      return
    }
    if (this.data.value2 == 0) {
      Toast.fail("请评价物流！")
      return
    }

    var self = this;
    wx.showLoading({
      title: "正在上传图片",
      mask: true,
    });
    var imglist = [];

    function uploadimg() {
      if (self.data.fileList.length > 0) {
        var imgpath = self.data.fileList.pop();
        uploadImage(
          imgpath.url,
          "images/",
          function (result) {
            console.log("======上传成功图片地址为：", result);
            imglist.push({
              "picUrl":result
            });
            //这个result就是返给你上传到oss上的地址链接
            uploadimg();
          },
          function (result) {
            console.log("======上传失败======", result);
            Toast.fail("上传失败");
            // wx.hideLoading();
            uploadimg();
          }
        );
      } else {
        service.addEvaluate({
          //spu标识
          "spuId": self.data.spuId,
          //订单商品表id   tb_orderDetail
          "orderDetAllId": self.data.orderDetAllId,
          //客户id
          "customerId": wx.getStorageSync("userInfo").id,
          //评分
          "score": self.data.value1,
          //物流评分
          "pdScore": self.data.value2,
          //评论内容
          "comments": self.data.message,
          //多张图片
          "commentPicList": imglist
        },res=>{
          if(res.code=="00000"){
            Toast({
              type: 'success',
              message: '评论成功！',
              onClose: () => {
                wx.navigateBack()
              },
            });
          }
        })
      }
    }
    uploadimg();
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})